import axios, {
  AxiosRequestHeaders,
  AxiosResponse,
  InternalAxiosRequestConfig,
  AxiosRequestConfig,
} from "axios";

import { layer } from "@layui/layui-vue";
import { ElLoading } from "element-plus";
import router from "@/router";

type TAxiosOption = {
  timeout: number;
  baseURL: string;
};

// export const uploadBaseURL = "http://10.129.60.31";
export const uploadBaseURL = "http://192.168.100.64";

const config: TAxiosOption = {
  timeout: 60000, 
  // baseURL: import.meta.env.DEV ? "/api/pc/" : "http://172.18.88.20/pc/",
  // baseURL: import.meta.env.DEV ? "/api/pc/" : "http://192.168.113.200/pc/",

  baseURL: import.meta.env.DEV ? "/api/pc/" : "http://192.168.100.64/pc/",
};
let loading: any = null;

let opt = {
  is_msg: true, //是否显示错误信息
  formData: "",
};

class Http {
  service;
  constructor(config: TAxiosOption) {
    this.service = axios.create(config);

    /* 请求拦截 */
    this.service.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        // console.log(config);
        // loading = layer.load(1);
        loading = ElLoading.service({
          lock: true, 
          text: "Loading",
          background: "rgba(0, 0, 0, 0.7)",
        });

        const token = localStorage.getItem("token");

        config.headers["saToken"] = token;

        if (opt.formData) {
          config["responseType"] = "blob";
        } else {
          config["responseType"] = "";
        }
        return config;
      },
      (error) => {
        console.log(error);

        return Promise.reject(error);
      }
    );

    /* 响应拦截 */
    this.service.interceptors.response.use(
      (response: AxiosResponse<any>) => {
        console.log(response);
        // layer.closeAll();
        loading.close();
        if (response.data.code !== 200) {
          // console.log(opt);
          if (opt.is_msg) {
            layer.msg(response.data.msg, { time: 1500, icon: 2 });
          }
          // console.log(response.data.code);

          if (response.data.code === 401) {
            router.push("/login");
          }
        }

        let retData: any = [];
        if (opt.formData) {
          retData = response;
        } else {
          retData = response.data;
        }
        return retData;
      },
      (error) => {
        console.log(error);
        if (opt.is_msg) {
          layer.msg(error.message, { time: 1500, icon: 2 });
        }
        loading.close();
        return Promise.reject(error);
      }
    );
  }

  /* GET 方法 */
  get<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.get(url, { params, ..._object });
  }
  /* POST 方法 */
  post<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.post(url, params, _object);
  }
  /* PUT 方法 */
  put<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.put(url, params, _object);
  }
  /* DELETE 方法 */
  delete<T>(url: string, params?: any, _object = {}): Promise<any> {
    return this.service.delete(url, { params, ..._object });
  }

  corsApi(
    url: string,
    method: string,
    params?: any,
    _object = { is_msg: true, formData: false }
  ) {
    // console.log(url, method, params, _object);
    opt.is_msg = _object.is_msg;
    if (_object.formData) {
      opt.formData = "application/x-www-form-urlencoded";
    } else {
      opt.formData = "";
    }

    const config: AxiosRequestConfig = {
      url,
      method,
      headers: {
        "Content-Type": "application/json",
      },
      options: _object,
    };
    if (method === "get" || method === "GET") {
      config.params = params;
    } else {
      config.data = params;
    }
    return this.service(config);
  }
}

export default new Http(config);
